package scoutobahn.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import scoutobahn.dijkstra.Dijkstra;
import scoutobahn.dijkstra.DijkstraNode;
import scoutobahn.highway.HighwayGraph;

/**
 * The model Class, managing the Algorithm and storing the Data it needs.
 * @author Florian
 *
 */
public class Model {
        private HighwayGraph highwayGraph;
        private List<ActionListener> actionListeners;
        private Collection<DijkstraNode> nodes;

        /**
         * Creates a new Model with empty members.
         */
        public Model() {
            highwayGraph = new HighwayGraph(null, null);
            actionListeners = new LinkedList<ActionListener>();
        }

        /**
         * Starts the Dijkstra algorithm.
         * @param startTown The name of the Town where the route starts.
         * @param endTown The name of the Town where the route should end.
         */
        public void runDijkstra(final String startTown, final String endTown) {
        Dijkstra dijkstra = new Dijkstra(highwayGraph);
        this.nodes =
                dijkstra.getRoute(startTown, endTown);

        for (ActionListener listener : actionListeners) {
            listener.actionPerformed(
                    new ActionEvent(this, 0, "djkstra finished"));
        }

        }

        /**
         * Gets the HighwayGraph stored in the Model.
         * @return The Model's HighwayGraph.
         */
        public HighwayGraph getHighwayGraph() {
            return highwayGraph;
        }

        /**
         * Sets the HighwayGraph stored in the Model.
         * @param newHighwayGraph The new HighwayGraph.
         */
        public void setHighwayGraph(final HighwayGraph newHighwayGraph) {
            this.highwayGraph = newHighwayGraph;
        }

        /**
         * Adds a new ActionListener to the Model's internal list
         * for notification, when the Algorithm has finished.
         * @param listener The new ActionListener.
         */
        public void addActionListener(final ActionListener listener) {
            actionListeners.add(listener);
        }
        /**
         * Returns the nodes that make up the calculated route.
         * @return A Collection of HighwayNodes
         */
        public Collection<DijkstraNode> getNodes() {
            return this.nodes;
        }
}
